package com.example.banksys;

import DataBase.DBMsg;
import com.pixelduke.control.skin.FXSkins;
import handler.queryHandler;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.ImageCursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.io.IOException;
import java.sql.SQLException;

import static com.example.banksys.MainApplication.isteller;
import static com.example.banksys.MainApplication.mystage;

public class Login {
    public TextField IDunput;
    public PasswordField passwdinput;
    public Button confirm;
    public Pane mypane;

    public void initialize(){
        confirm.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if(IDunput.getText().equals("")||passwdinput.getText().isEmpty()||passwdinput.getText().equals("")||IDunput.getText().isEmpty()){
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.titleProperty().set("提示");
                    alert.headerTextProperty().set("请输入正确用户名密码！");
                    alert.showAndWait();
                    return;
                }
                long idnum=Long.parseLong(IDunput.getText());
                String passwd=passwdinput.getText();
                queryHandler queryHandler=new queryHandler();
                try {
                    DBMsg res=queryHandler.employeeLogin(idnum,passwd);
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.titleProperty().set("信息");
                    alert.headerTextProperty().set(res.getInfo());
                    alert.showAndWait();
                    if(res.getState()== DBMsg.State.SUCCESS){
                        if(isteller){
                            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("tellerMainpage.fxml"));
                            Scene scene = new Scene(fxmlLoader.load());
                            scene.getStylesheets().add(FXSkins.getStylesheetURL());
                            //注意这里，是关键的获取stage的方式，和pane和scene是有极大关联的
                            Stage t= (Stage) mypane.getScene().getWindow();
                            t.setScene(scene);
                            t.setX(380);
                            t.setY(70);
                        }else{
                            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("managerMainpage.fxml"));
                            Scene scene = new Scene(fxmlLoader.load());
                            scene.getStylesheets().add(FXSkins.getStylesheetURL());
                            //注意这里，是关键的获取stage的方式，和pane和scene是有极大关联的
                            Stage t= (Stage) mypane.getScene().getWindow();
                            t.setScene(scene);
                            t.setX(380);
                            t.setY(70);
                        }

                    }else{
                        IDunput.setText("");
                        passwdinput.setText("");
                    }
                } catch (SQLException|IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }



}
